package com.neusoft.neuiotms.dcom.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.FunctionalUser;
import com.neusoft.neuiotms.dcom.model.QFunctionalUser;
import com.neusoft.neuiotms.dcom.repository.CustomizedFunctionalUserRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedFunctionalUserRepositoryImpl extends AbstractRepository
        implements CustomizedFunctionalUserRepository {

    @Override
    public Page<FunctionalUser> search(final Pageable pageable, final Searchable searchable) {

        final QFunctionalUser qFunctionalUser = QFunctionalUser.functionalUser;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<FunctionalUser> query = this.query().selectFrom(qFunctionalUser);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return FunctionalUser.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QFunctionalUser qFunctionalUser = QFunctionalUser.functionalUser;

        // 人员名称检索
        where.and(super.containsStr(qFunctionalUser.name, searchable, "name"));
        // 职能ID
        where.and(super.equalsLong(qFunctionalUser.group.id, searchable, "functionalGroupId"));
        return where;
    }
}
